package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.AppGridViewBinding;
import sz.szsmk.citizencard.databinding.ApphomeHeadViewNewBinding;
import sz.szsmk.citizencard.model.network.bean.response.HomeHeadFuncResponse;
import sz.szsmk.citizencard.model.network.bean.response.HomeWeathRes;
import sz.szsmk.citizencard.model.network.bean.response.OneWordRes;
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo;
import sz.szsmk.citizencard.ui.newversion.config.Constants;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.WeathUtils;
import sz.szsmk.citizencard.widget.BlurringView;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/18 16:27
 * description: App首页头部
 */
public class AppHomeHeadAdapter extends DelegateAdapter.Adapter<AppHomeHeadAdapter.AppHomeHeadHolder> {
    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private HomeWeathRes weathData;
    private ArrayList<String> mList = new ArrayList<>();
    private ArrayList<Integer> iconList = new ArrayList<>();
    private List<HomeHeadFuncResponse.FunctionListBean> viewData;
    private ApphomeHeadMyRvGridAdapter rvMyGridAdapter;
    private BaseInterface baseInterface;
    private AdInfo backAdInfo;
    private  int newCount=0;


    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, String id, HomeHeadFuncResponse.FunctionListBean bean);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    public AppHomeHeadAdapter(FragmentActivity activity, DefaultLayoutHelper headHelper) {
        mContext = activity;
        mGridLayoutHelper = headHelper;


    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public AppHomeHeadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_head_view_new, parent, false);
        return  AppHomeHeadHolder.creat(parent);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_head_view_new;
    }

    @Override
    public void onBindViewHolder(AppHomeHeadHolder holder, int position) {
//        holder.blurringView.setBlurredView();
        if (weathData != null) {
            holder.binding.weatherAir.setText("空气  " + weathData.getAirQuantity());
//            holder.weath1.setText(weathData.getWeather());
            holder.binding.weatherTempText.setText(weathData.getTemperature() + "℃");
            WeathUtils.setWeath(holder.binding.weatherTemp,weathData.getWeather());
        }
        holder.binding.layoutWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtils.jump(mContext, "weath", 0);
            }
        });

        if(newCount==0){
            holder.binding.messageNum.setVisibility(View.GONE);
        }else{
            holder.binding.messageNum.setVisibility(View.VISIBLE);
        }
        holder.binding.messageNum.setText(String.valueOf(newCount));
        holder.binding.searchEt.setFocusable(false);
        holder.binding.searchEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtils.jump(mContext, "SearchHotActivity", 0);
            }
        });

        holder.binding.messageIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    JumpUtils.jump(mContext, "HomeNews", 0);//TravelLineActivity
//                    JumpUtils.jump(mContext, Constants.CardListActivity, 0);
//                    JumpUtils.jump(mContext, "tripPlan", 0);
//                    JumpUtils.jump(mContext, "busCollectActivity", 0);

            }
        });
        if(backAdInfo!=null){
            Glide.with(mContext)
                    .load(backAdInfo.getResource_url())
                    .into(holder.binding.homeMainBack);
        }else{
//            holder.binding.homeMainBack.setBackgroundColor(R.color.color_2184FA);
        }

        loadItemData(position, holder.binding);

    }
    private void loadItemData(int position, ApphomeHeadViewNewBinding binding) {
        if (viewData != null) {
            RecyclerView appGridRv = binding.appGrid;
            GridLayoutManager manager = new GridLayoutManager(mContext, 4);
            rvMyGridAdapter = new ApphomeHeadMyRvGridAdapter(mContext,"head");


            rvMyGridAdapter.setBaseInterface(new ApphomeHeadMyRvGridAdapter.BaseInterface() {
                @Override
                public void onClick(View view, String url, String syslabl, int need_header, String id, HomeHeadFuncResponse.FunctionListBean bean) {

                    if (baseInterface!=null){
                        baseInterface.onClick(view,url,syslabl,need_header,id,bean);
                    }
                }
            });
            appGridRv.setLayoutManager(manager);
            appGridRv.setAdapter(rvMyGridAdapter);
            rvMyGridAdapter.setViewData(viewData);
            rvMyGridAdapter.notifyDataSetChanged();
        }

    }
    @Override
    public int getItemCount() {
        return 1;
    }

    public void setWeathData(HomeWeathRes weathData) {
        this.weathData = weathData;
    }
    public void setMainBack(AdInfo adInfo){this.backAdInfo=adInfo;}

    public void setViewData(List<HomeHeadFuncResponse.FunctionListBean> viewData) {
        this.viewData = viewData;
    }

    public void setNewsData(int i){
        this.newCount=i;
    }

    public static class AppHomeHeadHolder extends RecyclerView.ViewHolder {
        private ApphomeHeadViewNewBinding binding;

        public static AppHomeHeadHolder creat(ViewGroup parent) {
            ApphomeHeadViewNewBinding binding =
                    ApphomeHeadViewNewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);

            return new AppHomeHeadHolder(binding);
        }
//        private final BlurringView blurringView;
//        private final TextView weatherTtemp;
//        private final TextView weatherAir;
////        private final TextView tem;
//        private final RelativeLayout weatherLayout;
//        private final EditText search_et;

        public AppHomeHeadHolder(ApphomeHeadViewNewBinding binding) {
            super(binding.getRoot());

//            blurringView = (BlurringView) itemView.findViewById(R.id.blurring_view);
////            weath1 = (TextView) itemView.findViewById(R.id.home_weath);
//
//            weatherTtemp = (TextView) itemView.findViewById(R.id.weather_temp_text);
//            weatherAir = (TextView) itemView.findViewById(R.id.weather_air);
//
//            weatherLayout = (RelativeLayout) itemView.findViewById(R.id.layout_weather);
//            search_et = (EditText) itemView.findViewById(R.id.search_et);

            this.binding = binding;
        }

    }

}
